package com.example.function.integration.controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.util.DateUtils;
import com.example.function.integration.annotation.OperationType;
import com.example.function.integration.config.GlobalLocalCacheConfig;
import com.example.function.integration.response.ResponseBody;
import com.example.function.integration.service.FunctionTestService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author fengcheng
 * @version V1.0
 * @Description: 功能测试控制类
 * @Date 2024-09-30 17:40
 */
@RestController
@RequestMapping("/test")
public class FunctionTestController {

    @Resource
    public FunctionTestService functionTestService;

    public static Logger logger = LoggerFactory.getLogger(FunctionTestController.class);

    @OperationType(type = "query", desc = "测试Spring的AOP功能")
    @PostMapping("/testSpringAop")
    public Object testSpringAop(@RequestBody String queryParam) {
        logger.info("testSpringAop.aop逻辑已经完成开始初始化数据库数据:{}", queryParam);
        return true;
    }

    @OperationType(type = "query", desc = "测试全局本地缓存功能")
    @PostMapping("/testGlobalLocalCacheAdd")
    public Object testGlobalLocalCacheAdd(@RequestBody String queryParam) {
        logger.info("向全局缓存中存入一个对象,失效时间是10秒，参数: {}，执行时间:{}", queryParam, DateUtils.toString(new Date()));
        JSONObject jsonObject = JSONObject.parseObject(queryParam);
        GlobalLocalCacheConfig.put(jsonObject.getString("key1"), jsonObject.getString("value1"));
        try {
            Thread.sleep(5000);
            GlobalLocalCacheConfig.put(jsonObject.getString("key2"), jsonObject.getString("value2"));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "数据存入缓存成功";
    }

    @OperationType(type = "query", desc = "校验缓存中的对象是否过期")
    @PostMapping("/testGlobalLocalCacheCheck")
    public Object testGlobalLocalCacheCheck(@RequestBody String queryParam) {
        JSONObject jsonObject = JSONObject.parseObject(queryParam);
        String result = "缓存对象过期校验失败";
        Object cacheValue = GlobalLocalCacheConfig.get(jsonObject.getString("key1"));
        Object cacheValue1 = GlobalLocalCacheConfig.get(jsonObject.getString("key2"));
        System.out.println("key::::::" + cacheValue);
        System.out.println("key2::::::" + cacheValue1);
        if (ObjectUtils.isEmpty(cacheValue)) {
            result = "缓存对象过期校验成功";
        }
        logger.info("缓存过期校验结果，:{}", result);
        return result;
    }

    @OperationType(type = "query", desc = "测试全局异常捕获功能")
    @PostMapping("/testGlobalException")
    public ResponseBody testGlobalException() {
        logger.info("测试全局异常捕获功能");
        return ResponseBody.success(functionTestService.testGlobalException());
    }
}
